package componentes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Reynald0
 */
public class CompSwitch extends JPanel implements ActionListener
{
   private JLabel lEtiqueta;
   private JButton bBoton;
   private Boolean isPrendido;
   
   public CompSwitch(JPanel pPanel, Boolean pIsEncendido, ActionListener pAccion) {
      isPrendido = pIsEncendido;
      lEtiqueta = new JLabel(isPrendido?"ON":"OFF");
      bBoton = new JButton();
      this.setLayout(new BorderLayout());
      this.add(bBoton, BorderLayout.CENTER);
      this.add(lEtiqueta, isPrendido?BorderLayout.EAST:BorderLayout.WEST);  
      bBoton.addActionListener(pAccion);
      bBoton.addActionListener(this);
      if(pPanel != null) pPanel.add(this);
   }

   public void actionPerformed(ActionEvent pE) 
   {
      isPrendido = !isPrendido;
      lEtiqueta.setText(isPrendido? "ON": "OFF");
      this.add(lEtiqueta,isPrendido?BorderLayout.WEST: BorderLayout.EAST);
      this.setBackground( isPrendido? Color.GREEN: Color.RED);
   }
  
}